<template>
  <a-modal
    title="供应商资料详情"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
    :footer="null"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="供应商资料名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback>
              <a-input
                readOnly
                placeholder="请输入供应商资料名称"
                v-decorator="['suppDataName', {rules: [{required: true, message: '请输入供应商资料名称！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="社会统一信用代码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback>
              <a-input
                readOnly
                placeholder="请输入社会统一信用代码"
                v-decorator="['code', {rules: [{required: true, message: '请输入社会统一信用代码！'}]}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="供应商分类"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback>
              <a-tree-select
                :open="false"
                v-decorator="['suppSortId', {rules: [{ required: true, message: '请选择供应商分类！' }]}]"
                style="width: 100%"
                :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"
                :treeData="suppTree"
                placeholder="请选择供应商分类">
                <span slot="title" slot-scope="{ id }">{{ id }}</span>
              </a-tree-select>
            </a-form-item>
          </a-col>
        </a-row>
        <supp-person-table-form-item
          ref="suppPersonTable"
          :detailReadOnly="readOnly"
          :oldTableData="suppPersonList"
          @tableDataChange="(data) => {suppPersonList = data}"
          @add="({tableData})=>{ suppPersonList = tableData}"
          @del="({tableData})=>{ suppPersonList = tableData}"
        />
        <a-form-item
          label="备注信息"
          :labelCol="labelCol_T"
          :wrapperCol="wrapperCol_T"
          has-feedback
        >
          <a-textarea
            :auto-size="{ minRows: 3, maxRows: 6 }"
            placeholder="请输入备注信息"
            v-decorator="['remark']"
            readOnly
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import SuppPersonTableFormItem from '@/views/main/suppdata/suppPersonTableFormItem'
import { getSuppTree } from '@/api/modular/main/suppsort/suppSortManage'
import { suppPersonList } from '@/api/modular/main/suppperson/suppPersonManage'

export default {
    components: {
      SuppPersonTableFormItem
    },
    data () {
      return {
        readOnly: true,
        suppPersonList: [],
        allSuppPersonList: [],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        labelCol_T: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperCol_T: {
          xs: { span: 24 },
          sm: { span: 20 }
        },
        suppTree: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        suppSortIdData: []
      }
    },
    created() {
      this.getALLSuppPersonList()
    },
    methods: {
      // 初始化方法
      async detail (record) {
        this.visible = true
        const tempConPersonList = []
        const data = await this.getALLSuppPersonList()
        data.forEach(item => {
          if (item.suppDataId === record.id) {
            tempConPersonList.push(item)
          }
        })
        this.suppPersonList = [...tempConPersonList]
        this.getSuppTree()
        this.$nextTick(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              suppDataName: record.suppDataName,
              code: record.code,
              suppSortId: record.suppSortId,
              remark: record.remark
            }
          )
        })
      },
      async getALLSuppPersonList() {
        this.allSuppPersonList = []
        var promise = suppPersonList().then(res => {
          this.allSuppPersonList = res.data
        })
        await promise
        return this.allSuppPersonList
      },
      /**
       * 获取类型树，并加载于表单中
       */
      getSuppTree() {
        getSuppTree().then((res) => {
          this.formLoading = false
      if (!res.success) {
            this.suppTree = []
          return
        }
          this.suppTree = res.data
        })
      },
      handleCancel () {
        this.suppPersonList = []
        this.form.resetFields()
        this.visible = false
      }
    },
    watch: {
      // 监听suppPersonList
      suppPersonList: {
        handler() {
          this.$nextTick(() => {
            this.$refs.suppPersonTable.refreshTable(this.suppPersonList)
          })
        }
      }
    }
  }
</script>
